<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .main {
        	padding: 15px 15px 85px;
        }
        .main ul li {
        	margin-bottom: 15px;
        }
        .main ul li .li_box {
        	height: 68px;
        	line-height: 68px;
        	background-color: #fff;
        	border-radius: 10px;
        	display: -webkit-box;
        	display: -webkit-flex;
        	align-items: center;
        	padding: 0 15px;
        }
        .main ul li .li_box .li_boxl {
        	width: 0;
            height: 68px;
        	flex: 1;
        	-webkit-flex: 1;
        	box-flex: 1;
        	-webkit-box-flex: 1;
        	overflow: hidden;
        	text-overflow: ellipsis;
        	white-space: nowrap;
        }
        .main ul li .li_box .li_boxc {
            height: 68px;
            display: -webkit-box;
            -webkit-box-align: center;
        }
        .main ul li .li_box .li_boxc img {
			width: 15px;
        	padding: 0 10px;
        	display: block;
        }
        .main ul li .li_box .li_boxr {
            height: 68px;
            display: -webkit-box;
            -webkit-box-align: center;
        }
        .main ul li .li_box .li_boxr img {
        	width: 15px;
        	padding: 0 10px;
        	display: block;
        }
        #footer {
            width: 100%;
            padding: 15px;
            box-sizing: border-box;
            position: fixed;
            bottom: 0;
            background-color: #f8f8f8;
            z-index: 99;
        }
        #footer .btn {
            width: 100%;
            height: 40px;
            line-height: 40px;
            background-color: #e62442;
            border-radius: 5px;
            font-size: 15px;
            color: #fff;
            display: -webkit-box;
            -webkit-box-align: center;
            -webkit-box-pack: center;
        }
        #footer .btn img {
            height: 22px;
            display: block;
            margin-right: 7px;
        }
        .without_data{
            display: none;
            color:#ccc;
            text-align:center;
            padding-top: 65%;
        }
        .hidden_img {
            display: none;
        }
    </style>
</head>
<body>
    <div class="main">
        <div class="without_data">暂无优惠</div>
    	<ul id="data_ul">
 <!--    		<li>
    			<div class="li_box">
    				<div class="li_boxl">消费满20元减10元</div>
    				<div class="li_boxc">
    					<img src="../image/coupon_del.png">
    				</div>
    				<div class="li_boxr" tapmode onclick="fnEditCondition();">
    					<img src="../image/coupon_edit.png">
    				</div>
    			</div>
    		</li> -->

    	</ul>
    </div>
    <footer id="footer">
        <div class="btn" tapmode onclick="fnAddCondition();">
            <img src="../image/add_coupon_icon.png">
            添加条件
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script id="dataTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <li>
            <div class="li_box">
                <div class="li_boxl">消费满{{=it[i]['total_price']}}元减{{=it[i]['reduce_price']}}元</div>
                <div class="li_boxc">
                    <img src="../image/coupon_del.png" onclick="fnDeleteCoupon({{=it[i]['id']}},this);" tapmode>
                </div>
                <div class="li_boxr" tapmode onclick="fnEditCondition({{=it[i]['id']}});">
                    <img src="../image/coupon_edit.png">
                </div>
            </div>
        </li>
    {{ }; }}
</script>
<script type="text/javascript">
    apiready = function(){
        shop = $api.getStorage('shop_data');

        //下拉刷新
        api.setCustomRefreshHeaderInfo({
            bgColor: '#f8f8f8',
            loadAnimInterval:60,
            image: {
               pull: pullArr,
               load: loadArr
            }
        }, function(ret, err) {
            fnGetCoupon(1);
        });

        //监听优惠卷变化
        api.addEventListener({
            name: 'couponChangeEvent'
        }, function(ret, err) {
            if (ret) {
                fnGetCoupon(1);
            }
        });
        fnGetCoupon();
    };

    // 编辑条件
    function fnEditCondition(id) {
        api.openWin({
            name: 'add_condition',
            url: 'add_condition.html',
            pageParam : {
                id:id
            }
        });
    }

    // 添加条件
    function fnAddCondition() {
        api.openWin({
            name: 'add_condition',
            url: 'add_condition.html'
        });
    }

    function fnGetCoupon(type) {
        api.ajax({
            url: rootPath+'/api/seller/get_coupon',
            method: 'get',
            cache: true,
            data: {
                values: { 
                    shop_id : shop.id,
                    token:shop.token,
                },
            }
        }, function(ret, err) {
            //type=1 下拉刷新，清除原来数据
            if(type == 1) {
                $('#data_ul').empty();
            }
            if (ret) {
                if(ret.status == 200) {
                    $('.without_data').hide();
                    appendDotData(ret.data,'dataTpl','data_ul',true);
                }else {
                    $('.without_data').show();
                }
            } else {
                alert(JSON.stringify(err.msg))
            }
            if(type == 1) {
                // 是下拉刷新的，要收回
                api.refreshHeaderLoadDone();
            }
            if(type == 2) {
                //上拉给点假时间,好看些         
                setTimeout(function(){
                    $(".loading").hide();
                },1000);
            }else {
                $('.loading').hide();

            }
            closeLoading();
        });
    }

    function fnDeleteCoupon(id,this_) {
        api.ajax({
            url: rootPath+'/api/seller/delete_coupon',
            method: 'post',
            data: {
                values: { 
                    shop_id: shop.id,
                    token: shop.token,
                    id : id
                },
            }
        }, function(ret, err) {
            if (ret) {
                if(ret.status == 200) {
                    api.toast({
                        msg: '删除成功',
                        location:'middle'
                    });
                    $(this_).parents('li').remove();
                }
            } else {
                alert(JSON.stringify(err.msg));
            }
        });
    }
</script>
</html>